﻿using DotNetSpeech;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using WeighClient.Model;
using WeighClient.Utils;


namespace WeighClient.Windows
{
    /// <summary>
    /// NodeRed.xaml 的交互逻辑
    /// </summary>
    public partial class NodeRed : Window
    {
        SynchronizationContext _syncContext = null;
        delegate void MyDelegate(Object value);
        SpVoice speech = null;
        private IModel channel = null;
        private String TextProperty1 = String.Empty;
        private DispatcherTimer timer;

        /// <summary>
        /// 连接状态
        /// </summary>
        public bool bConnState_Left = false;
        public bool bConnState_Right = false;

        /// <summary>
        /// 端口句柄
        /// </summary>
        public IntPtr pHandle_Left = IntPtr.Zero;
        public IntPtr pHandle_Right = IntPtr.Zero;

        public string sReaderIP_Left = App.GetConfig().Left_RFID_sReaderIP;
        int iReaderPoint_Left = int.Parse(App.GetConfig().Left_RFID_iReaderPoint);

        public string sReaderIP_Right = App.GetConfig().Right_RFID_sReaderIP;
        int iReaderPoint_Right = int.Parse(App.GetConfig().Right_RFID_iReaderPoint);

        /// <summary>
        /// 读卡状态
        /// </summary>
        bool bReadCodeState = false;
        /// <summary>
        /// 读取卡信息的线程
        /// </summary>
        Thread readThread_Left = null;
        Thread readThread_Right = null;

        private int m_ledType;
        private int m_ledWidth;
        private int m_ledHeight;
        private int m_ledColor;
        private int m_ledGrayLevel;

        Hashtable rfidaCodeMap;

        public NodeRed()
        {
            rfidaCodeMap = new Hashtable();

            m_ledType = 0;
            m_ledWidth = 64;
            m_ledHeight = 48;
            m_ledColor = 1;
            m_ledGrayLevel = 0;
            InitializeComponent();
            _syncContext = SynchronizationContext.Current;
            this.Loaded += Window_Loaded;
            this.Unloaded += Window_UnLoaded;

            speech = new SpVoice();

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(timeCycle);
            timer.Start();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            txtDeviceCode.Text = App.GetConfig().DeviceCode;

            HttpServer server = App.GetHttpServer();
            if (null != server)
            {
                HttpServer.sendMsgEvent += Client_sendMsgEvent;
            }
            //连接消息队列
            var factory = new ConnectionFactory();
            factory.HostName = App.GetConfig().Mq_HostName;
            factory.UserName = App.GetConfig().Mq_UserName;
            factory.Password = App.GetConfig().Mq_Password;
            var connection = factory.CreateConnection();
            channel = connection.CreateModel();

            subscriptionMQ();

            if (App.GetConfig().AutoWeigh)
            {
                labelAutoWeigh.Text = "自动计量";
            }
            else
            {
                labelAutoWeigh.Text = "手动计量";
            }

            //获取监控端操作员身份信息
            JObject jsonParamObj = new JObject();
            jsonParamObj.Add("from", "client");
            jsonParamObj.Add("clientId", App.GetConfig().DeviceCode);
            jsonParamObj.Add("type", "GetLoginUserInfo");
            SendMqMessage(jsonParamObj);

            sendClientAutoWeigh();
            this.sendMsg("齐润化工", "欢 迎 您", false, "left");
            this.sendMsg("齐润化工", "欢 迎 您", false, "right");
            //读取RFID卡
            bool bInit_Left = false;
            bInit_Left = RfidApi.SAAT_TCPInit(out pHandle_Left, sReaderIP_Left, iReaderPoint_Left);
            if (!bInit_Left)
            {
                _syncContext.Post(AddLogMessage, new LogInfo("RFID初始化失败", true));
                return;
            }
            //2.打开连接
            bConnState_Left = RfidApi.SAAT_Open(pHandle_Left);
            if (!bConnState_Left)
            {
                _syncContext.Post(AddLogMessage, new LogInfo("左RFID连接读卡器失败", true));
                return;
            }

            //读取RFID卡
            bool bInit_Right = false;
            bInit_Right = RfidApi.SAAT_TCPInit(out pHandle_Right, sReaderIP_Right, iReaderPoint_Right);
            if (!bInit_Right)
            {
                _syncContext.Post(AddLogMessage, new LogInfo("RFID初始化失败", true));
                return;
            }
            //2.打开连接
            bConnState_Right = RfidApi.SAAT_Open(pHandle_Right);
            if (!bConnState_Right)
            {
                _syncContext.Post(AddLogMessage, new LogInfo("右RFID连接读卡器失败", true));
                return;
            }
            this.beginReadRFID(new JObject());


        }
        private void Window_UnLoaded(object sender, RoutedEventArgs e) {
            if (readThread_Left != null)
            {
                if (readThread_Left.IsAlive)
                {
                    readThread_Left.Abort();
                    readThread_Left.Join();
                }
            }
            if (readThread_Right != null)
            {
                if (readThread_Right.IsAlive)
                {
                    readThread_Right.Abort();
                    readThread_Right.Join();
                }
            }

            // 停止读卡
            RfidApi.SAAT_PowerOff(pHandle_Left);
            RfidApi.SAAT_PowerOff(pHandle_Right);

            bConnState_Left = !CloseConnLeft();
            if (bConnState_Left)
            {
                //MessageBox.Show("关闭读卡器连接失败!");
                return;
            }
            bConnState_Right = !CloseConnRight();
            if (bConnState_Right)
            {
                //MessageBox.Show("关闭读卡器连接失败!");
                return;
            }
        }
        //订阅集控端消息
        private void subscriptionMQ()
        {
            var consumer = new EventingBasicConsumer(channel);//消费者

            channel.BasicConsume("Queue_" + App.GetConfig().DeviceCode + "_Server", true, consumer);//消费消息
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body.ToArray());

                JObject jsonObj = JObject.Parse(message);
                string from = (string)jsonObj.GetValue("from");
                if ("monitor".Equals(from))
                {
                    Server_sendMsgEvent(jsonObj);
                }
            };
        }
        private void Client_sendMsgEvent(Object obj)
        {
            MyDelegate d = new MyDelegate(DealClientMessage);
            this.Dispatcher.Invoke(d, obj);
        }
        private void Server_sendMsgEvent(Object obj)
        {
            MyDelegate d = new MyDelegate(DealServerMessage);
            this.Dispatcher.Invoke(d, obj);
        }
        void DealClientMessage(Object obj)
        {
            JObject jsonObj = JObject.Parse(obj.ToString());
            String type = (String)jsonObj.GetValue("type");

            if ("Weigh".Equals(type))
            {
                //获取重量
                JObject detailObj = (JObject)jsonObj.GetValue("detail");
                String weigh = (String)detailObj.GetValue("weigh");
                String isSteady = (String)detailObj.GetValue("isSteady");

                _syncContext.Post(SetWeigh, weigh);
                _syncContext.Post(SetSteady, isSteady);

                //向集控端发送实时重量
                if ("1".Equals(isSteady))
                {
                    isSteady = "稳定";
                }
                else
                {
                    isSteady = "不稳定";
                }
                JObject jsonParamObj = new JObject();
                jsonParamObj.Add("from", "client");
                jsonParamObj.Add("clientId", App.GetConfig().DeviceCode);
                jsonParamObj.Add("type", "Weight");
                jsonParamObj.Add("weight", weigh);
                jsonParamObj.Add("steady", isSteady);

                //_syncContext.Post(SendMqMessage, jsonParamObj);
                SendMqMessage(jsonParamObj);
            }
            else if ("PersonWeigh".Equals(type))
            {
                //获取重量
                JObject detailObj = (JObject)jsonObj.GetValue("detail");
                String weigh = (String)detailObj.GetValue("weigh");
                String isSteady = (String)detailObj.GetValue("isSteady");

                _syncContext.Post(SetPersonWeigh, weigh);
                _syncContext.Post(SetPersonSteady, isSteady);

                //向集控端发送实时重量
                if ("1".Equals(isSteady))
                {
                    isSteady = "稳定";
                }
                else
                {
                    isSteady = "不稳定";
                }
                JObject jsonParamObj = new JObject();
                jsonParamObj.Add("from", "client");
                jsonParamObj.Add("clientId", App.GetConfig().DeviceCode);
                jsonParamObj.Add("type", "PersonWeight");
                jsonParamObj.Add("weight", weigh);
                jsonParamObj.Add("steady", isSteady);

                //_syncContext.Post(SendMqMessage, jsonParamObj);
                SendMqMessage(jsonParamObj);
            }
            else if ("GetCarNo".Equals(type))
            {
                //获取车号
                JObject detailObj = (JObject)jsonObj.GetValue("detail");
                String carNumber = (String)detailObj.GetValue("carNumber");
                String carDirection = (String)detailObj.GetValue("carDirection");

                _syncContext.Post(SetBillInfo, new Bill());

                App.GetRunTimeVariable().CarNo = carNumber;
                _syncContext.Post(SetCarNo, carNumber);
                _syncContext.Post(AddLogMessage, new LogInfo("车号:" + carNumber+"请准备称重"));

                //向集控端发送车牌号
                JObject jsonParamObj = new JObject();
                jsonParamObj.Add("from", "client");
                jsonParamObj.Add("clientId", App.GetConfig().DeviceCode);
                jsonParamObj.Add("type", "GetCarNo");
                jsonParamObj.Add("carNo", carNumber);

                //_syncContext.Post(SendMqMessage, jsonParamObj);
                SendMqMessage(jsonParamObj);
                this.sendMsg(carNumber, "请刷卡称重", false, "left");
                this.sendMsg(carNumber, "请刷卡称重", false, "right");
            }
            else if ("GetRFIDCarNo".Equals(type))
            {
                //获取车号
                JObject detailObj = (JObject)jsonObj.GetValue("detail");
                String carNumber = (String)detailObj.GetValue("carNumber");
                //App.GetRunTimeVariable().CarNo = carNumber;
                //_syncContext.Post(SetCarNo, carNumber);
                //_syncContext.Post(AddLogMessage, new LogInfo("获取车号:" + carNumber));

                //向集控端发送车牌号
                JObject jsonParamObj = new JObject();
                jsonParamObj.Add("from", "client");
                jsonParamObj.Add("clientId", App.GetConfig().DeviceCode);
                jsonParamObj.Add("type", "GetRFIDCarNo");
                jsonParamObj.Add("carNo", carNumber);
                if (App.getGlobalVariables().RfidCarNumber == null || App.getGlobalVariables().RfidCarNumber.Equals(""))
                {
                    App.getGlobalVariables().RfidCarNumber = carNumber;
                    _syncContext.Post(AddLogMessage, new LogInfo("车载卡:" + carNumber));
                }
                else if (!App.getGlobalVariables().RfidCarNumber.Equals(carNumber))
                {
                    App.getGlobalVariables().RfidCarNumber = carNumber;
                    _syncContext.Post(AddLogMessage, new LogInfo("车载卡:" + carNumber));
                }
                //_syncContext.Post(AddLogMessage, new LogInfo("车载卡:" + carNumber));
                //_syncContext.Post(SendMqMessage, jsonParamObj);
                SendMqMessage(jsonParamObj);
                this.sendMsg("车载卡", carNumber, false,"left");
                this.sendMsg("车载卡", carNumber, false, "right");

            }
            else if ("readRFID".Equals(type))
            {
                _syncContext.Post(beginReadRFID, new JObject());
            }
            else if ("stopReadRFID".Equals(type))
            {
                _syncContext.Post(stopReadRFID, new JObject());
            }
            else if ("LDStatus".Equals(type))
            {
                JObject detailObj = (JObject)jsonObj.GetValue("detail");
                String leftStatus = (String)detailObj.GetValue("leftStatus");
                String rightStatus = (String)detailObj.GetValue("rightStatus");

                //向集控端发送雷达状态
                JObject jsonParamObj = new JObject();
                jsonParamObj.Add("from", "client");
                jsonParamObj.Add("clientId", App.GetConfig().DeviceCode);
                jsonParamObj.Add("type", "LDStatus");
                jsonParamObj.Add("leftStatus", leftStatus);
                jsonParamObj.Add("rightStatus", rightStatus);

                _syncContext.Post(SendMqMessage, jsonParamObj);
                SendMqMessage(jsonParamObj);
                //_syncContext.Post(AddLogMessage, new LogInfo(leftStatus + "," + rightStatus));
            }
            else if ("PostVoiceMessage".Equals(type))
            {
                String message = (String)jsonObj.GetValue("message");
                //_syncContext.Post(AddVoiceLogTxt, message);
                if (jsonObj.ContainsKey("warning"))
                {
                    _syncContext.Post(AddLogMessage, new LogInfo(message, true));
                }
                else
                {
                    _syncContext.Post(AddLogMessage, new LogInfo(message));
                }
                //向集控端发送信息
                JObject jsonParamObj = new JObject();
                jsonParamObj.Add("from", "client");
                jsonParamObj.Add("clientId", App.GetConfig().DeviceCode);
                jsonParamObj.Add("type", "VoiceAndLed");
                jsonParamObj.Add("voiceMsg", message);

                //_syncContext.Post(SendMqMessage, jsonParamObj);
                SendMqMessage(jsonParamObj);

            }
            else if ("PostMessage".Equals(type))
            {
                String message = (String)jsonObj.GetValue("message");
                if (jsonObj.ContainsKey("warning"))
                {
                    _syncContext.Post(AddLogMessage, new LogInfo(message, true));
                }
                else
                {
                    _syncContext.Post(AddLogMessage, new LogInfo(message));
                }
            }
            else if ("PostError".Equals(type))
            {
                String message = (String)jsonObj.GetValue("message");
                String devalue = (String)jsonObj.GetValue("devalue");

                _syncContext.Post(AddLogMessage, new LogInfo(message, true));

                //向集控端发送信息
                JObject jsonParamObj = new JObject();
                jsonParamObj.Add("from", "client");
                jsonParamObj.Add("clientId", App.GetConfig().DeviceCode);
                jsonParamObj.Add("type", "Error");
                jsonParamObj.Add("message", message);
                jsonParamObj.Add("devalue", devalue);

                //_syncContext.Post(SendMqMessage, jsonParamObj)
                SendMqMessage(jsonParamObj);

            }
            else if ("LoadBillInfo".Equals(type))
            {
                JObject detailJSON = (JObject)jsonObj.GetValue("detail");
                //_syncContext.Post(AddLogTxt, detailObj.ToString());

                Bill bill = Json2Bill(detailJSON);
                _syncContext.Post(SetBillInfo, bill);

                //向集控端发送提单信息
                JObject jsonParamObj = new JObject();
                jsonParamObj.Add("from", "client");
                jsonParamObj.Add("clientId", App.GetConfig().DeviceCode);
                jsonParamObj.Add("type", "BillInfo");
                jsonParamObj.Add("billObj", JObject.FromObject(bill));

                //_syncContext.Post(SendMqMessage, jsonParamObj);
                SendMqMessage(jsonParamObj);

                _syncContext.Post(SetBillInfo, bill);
            }
            else if ("UploadBillSuccess".Equals(type))
            {
                JObject detailJSON = (JObject)jsonObj.GetValue("detail");
                Bill bill = Json2Bill(detailJSON);
                String carDirection = (String)jsonObj.GetValue("carDirection");
                //Bill bills = queryBillDetail(bill.id);
                //bill.pizhong = bills.pizhong;
                //bill.maozhong = bills.maozhong;
                //bill.jingzhong = bills.jingzhong;
                _syncContext.Post(SetBillInfo, bill);
                string weigh = bill.weigh;
                String txt1 = "";
                String txt2 = "";
                if (bill.print == "0")
                {
                    if (bill.loadPlace == "-1")
                    {
                        txt1 = bill.carNo;
                        txt2 = "皮重" + bill.weigh + "吨";
                    }
                    else
                    {
                        txt1 = bill.carNo;
                        txt2 = "皮重" + bill.weigh + "吨,请前往" + bill.loadPlace + "装车";
                    }
                    this.sendMsg(txt1, txt2, true, "left");
                    this.sendMsg(txt1, txt2, true, "right");
                } else if (bill.print=="1")
                {
                    String jz = "";
                    jz = Convert.ToDouble(bill.jingzhong).ToString("0.00");
                    txt1 =  bill.carNo ;
                    txt2 = "毛重" + bill.weigh + "吨,皮重" + bill.pizhong + "吨,净重" + jz + "吨";
                    this.sendMsg(txt1, txt2, true, "left");
                    this.sendMsg(txt1, txt2, true, "right");
                }
                 
                
                //向集控端发送提单信息
                JObject jsonParamObj = new JObject();
                jsonParamObj.Add("from", "client");
                jsonParamObj.Add("clientId", App.GetConfig().DeviceCode);
                jsonParamObj.Add("type", "BillInfo");
                jsonParamObj.Add("billObj", JObject.FromObject(bill));

                //_syncContext.Post(SendMqMessage, jsonParamObj);
                SendMqMessage(jsonParamObj);

                Thread.Sleep(500);

                //向集控端发送信息
                jsonParamObj = new JObject();
                jsonParamObj = new JObject();
                jsonParamObj.Add("from", "client");
                jsonParamObj.Add("clientId", App.GetConfig().DeviceCode);
                jsonParamObj.Add("type", "Complete");
                jsonParamObj.Add("message", txt1+txt2);
                jsonParamObj.Add("billId", bill.id);
                //_syncContext.Post(SendMqMessage, jsonParamObj);
                SendMqMessage(jsonParamObj);

                if (App.GetConfig().IfPrint.Equals("0"))
                {
                    if (bill.print == "1")
                    {
                        //打印
                        _syncContext.Post(AddLogMessage, new LogInfo("正在打印称重计量单"));
                        JObject jsonObj1 = new JObject();
                        jsonObj1.Add("from", "client");
                        jsonObj1.Add("type", "AutoPrintBill");
                        jsonObj1.Add("billId", bill.id);
                        jsonObj1.Add("clientId", App.GetConfig().DeviceCode);
                        _syncContext.Post(SendMqMessagePrint, jsonObj1);
                        //PrintUtils.printWeighOrder(bill);
                    }
                }

                _syncContext.Post(AddLogMessage, new LogInfo("车号"+txt1+txt2));
            }
            else if ("GetLoginUserInfo".Equals(type))
            {
                GetLoginUserInfo();
            }
            else if ("Complete".Equals(type))
            {
                _syncContext.Post(SetCarNo, "");
                _syncContext.Post(SetBillInfo, new Bill());
                App.GetRunTimeVariable().CarNo = "";
                _syncContext.Post(AddLogMessage, new LogInfo("等待称重"));

                JObject jsonPramObj = new JObject();
                jsonPramObj.Add("from", "client");
                jsonPramObj.Add("clientId", App.GetConfig().DeviceCode);
                jsonPramObj.Add("type", "ClearBillInfo");
                jsonPramObj.Add("message", "计量完成");

                _syncContext.Post(SendMqMessage, jsonPramObj);
                this.sendMsg("齐润化工", "欢 迎 您", false, "left");
                this.sendMsg("齐润化工", "欢 迎 您", false, "right");
                rfidaCodeMap.Clear();

            }
            else if ("uploadPic".Equals(type))
            {

                JObject jsonPramObj = new JObject();
                jsonPramObj.Add("from", "client");
                jsonPramObj.Add("clientId", App.GetConfig().DeviceCode);
                jsonPramObj.Add("type", "uploadPic");
                jsonPramObj.Add("message", "上传截图");

                _syncContext.Post(SendMqMessage, jsonPramObj);
            }

        }
        Bill Json2Bill(JObject detailJSON)
        {
            String id = (String)detailJSON.GetValue("id");
            String orderCode = (String)detailJSON.GetValue("billNo");
            String carNo = (String)detailJSON.GetValue("carNumber");
            String productName = (String)detailJSON.GetValue("productName");
            String corpName = (String)detailJSON.GetValue("corpName");
            String position = (String)detailJSON.GetValue("position");
            String planNum = (String)detailJSON.GetValue("planNum");
            String _guid = (String)detailJSON.GetValue("guid");
            String weigh = (String)detailJSON.GetValue("weigh");
            String personWeigh01 = (String)detailJSON.GetValue("personWeight01");
            String personWeigh02 = (String)detailJSON.GetValue("personWeight02");
            String loadPlaceName = (String)detailJSON.GetValue("loadPlaceName");
            String materialName = (String)detailJSON.GetValue("materialName");
            String cpzTime = (String)detailJSON.GetValue("cpzTime");
            String customerName = (String)detailJSON.GetValue("corpName");
            String fnpolicemsg = (String)detailJSON.GetValue("fnpolicemsg");
            Bill bill = new Bill();

            bill.id = id;
            bill.orderCode = orderCode;
            bill.carNo = carNo;
            bill.productName = productName;
            bill.corpName = corpName;
            bill.position = position;
            bill.planNum = planNum;
            bill.videoCarNo = App.GetRunTimeVariable().CarNo;
            bill.personWeight01 = personWeigh01;
            bill.personWeight02 = personWeigh02;
            bill.loadPlace = loadPlaceName;
            bill.cpzTime = cpzTime;
            bill.customerName = customerName;
            bill.fnpolicemsg = fnpolicemsg;

            bill.weightStatus = (String)detailJSON.GetValue("weightType");
            bill.yuanfaNum = (String)detailJSON.GetValue("yuanfaNum");
            bill.chazhi = (String)detailJSON.GetValue("chazhi");
            bill.fayou = (String)detailJSON.GetValue("fayou");
            bill.pizhong = (String)detailJSON.GetValue("pizhong");
            bill.maozhong = (String)detailJSON.GetValue("maozhong");
            bill.jingzhong = (String)detailJSON.GetValue("jingzhong");
            bill.qianbiao = (String)detailJSON.GetValue("qianbiao");
            bill.houbiao = (String)detailJSON.GetValue("houbiao");
            bill.busitype = (String)detailJSON.GetValue("busitype");
            bill.operatorName = (String)detailJSON.GetValue("operatorName");
            bill.print = (String)detailJSON.GetValue("print");
            bill.guid = _guid;
            bill.weigh= (String)detailJSON.GetValue("weigh");
            
            bill.materialName = materialName;

            return bill;
        }
        private Bill queryBillDetail(string id)
        {
            JObject paramObject = new JObject();
            paramObject.Add("id", id);
            Bill bill = new Bill();

            try
            {
                String serverIp = App.GetConfig().HttpServerIp;
                String serverPort = App.GetConfig().HttpServerPort;
                String response = HttpPostHelper.doHttpPost("http://" + serverIp + ":" + serverPort + "/ytBill/queryDetail2", paramObject.ToString());
                JObject rspJson = JObject.Parse(response);
                string code = rspJson["code"].ToString();
                if (code.ToLower() == "200")
                {
                    var jsonStr = rspJson["data"].ToString();
                    JObject jobj = JsonConvert.DeserializeObject<JObject>(jsonStr);
                    bill.pizhong = (String)jobj.GetValue("piWeight");
                    bill.maozhong = (String)jobj.GetValue("maoWeight");
                    bill.jingzhong = (String)jobj.GetValue("netWeight");
                }
            }
            catch (Exception ex)
            {

            }

            return bill;
        }
        void GetLoginUserInfo()
        {
            //获取监控端操作员身份信息
            JObject jsonParamObj = new JObject();
            jsonParamObj.Add("from", "client");
            jsonParamObj.Add("clientId", App.GetConfig().DeviceCode);
            jsonParamObj.Add("type", "GetLoginUserInfo");
            SendMqMessage(jsonParamObj);
        }
        void DealServerMessage(Object obj)
        {
            JObject jsonObj = JObject.Parse(obj.ToString());
            String type = (String)jsonObj.GetValue("type");
            if ("AutoWeigh".Equals(type))
            {
                String autoWeigh = (String)jsonObj.GetValue("value");
                if ("true".Equals(autoWeigh))
                {
                    App.GetConfig().AutoWeigh = true;
                    labelAutoWeigh.Text = "自动计量";
                }
                else
                {
                    App.GetConfig().AutoWeigh = false;
                    labelAutoWeigh.Text = "手动计量";
                }
                JObject reqObj = new JObject();
                reqObj.Add("type", "AutoWeigh");
                reqObj.Add("value", autoWeigh);
                SendNodeRedMessage("PostMessage/AutoWeighChange", reqObj);
            }
            else if ("ReadCard".Equals(type))
            {
                JObject reqObj = new JObject();
                SendNodeRedMessage("PostMessage/ReadCard", reqObj);
            }
            else if ("SaveWeight".Equals(type))
            {
                JObject reqObj = new JObject();
                SendNodeRedMessage("PostMessage/SaveWeigh", reqObj);
            }
            else if ("DealError".Equals(type))
            {
                JObject reqObj = new JObject();
                SendNodeRedMessage("PostMessage/DealError", reqObj);
            }
            else if ("ServerOperatorInfo".Equals(type))
            {
                JObject reqObj = new JObject();
                JObject operatorObj = new JObject();
                String id = (String)jsonObj.GetValue("loginUserId");
                String name = (String)jsonObj.GetValue("loginUserName");
                operatorObj.Add("id", id);
                operatorObj.Add("name", name);
                reqObj.Add("operator", operatorObj);

                JObject deviceObj = new JObject();
                deviceObj.Add("code", App.GetConfig().DeviceCode);
                deviceObj.Add("name", App.GetConfig().DeviceName);
                reqObj.Add("device", deviceObj);

                SendNodeRedMessage("PostMessage/ServerOperatorInfo", reqObj);
            }
            else if ("UploadClientAutoWeigh".Equals(type))
            {
                JObject reqObj = new JObject();
                //SendNodeRedMessage("PostMessage/UploadClientAutoWeigh", reqObj);
                sendClientAutoWeigh();
            }
            else if ("ChangeDoorStatus".Equals(type))
            {
                JObject reqObj = new JObject();
                String inOut = (String)jsonObj.GetValue("inOut");
                String status = (String)jsonObj.GetValue("status");
                reqObj.Add("inOut", inOut);
                reqObj.Add("status", status);
                SendNodeRedMessage("PostMessage/ChangeDoorStatus", reqObj);
            }
            else if ("PrintSmallTicket".Equals(type))
            {
                JObject billObj = (JObject)jsonObj.GetValue("billObj");
                Bill bill = Json2Bill(billObj);
                if (App.GetConfig().IfPrint.Equals("0"))
                {
                    //PrintUtils.printWeighOrder(bill);
                }
                
            }
            else if ("returnCard".Equals(type))
            {
                JObject reqObj = new JObject();
                SendNodeRedMessage("PostMessage/ReturnCard", reqObj);
            }
            else if ("printWeighOrder".Equals(type))
            {
                JObject billObj = (JObject)jsonObj.GetValue("billobj");
                Bill bill = new Bill();
                bill.carNo = (String)billObj.GetValue("CarNumber");
                bill.pizhong = (String)billObj.GetValue("PiWeight");
                bill.maozhong = (String)billObj.GetValue("MaoWeight");
                bill.jingzhong = (String)billObj.GetValue("NetWeight");
                bill.cpzTime = (String)billObj.GetValue("CpzTime");
                bill.cmzTime = (String)billObj.GetValue("CmzTime");
                //Bill bill = Json2Bill(billObj);
                if (App.GetConfig().IfPrint.Equals("0"))
                {
                    //PrintUtils.printWeighOrder(bill);
                }
            }
            else if ("WorkComplete".Equals(type))
            {
                JObject reqObj = new JObject();
                SendNodeRedMessage("PostMessage/WorkComplete", reqObj);
            }
        }
        private void SetCarNo(object text)
        {
            this.txtCarNo.Text = text.ToString();
            //this.txtLog.Text = "获取车号：" + text.ToString() + "\n";
            //txtVideoCarNo
        }
        private void SetWeigh(object text)
        {
            if (text == null)
            {
                this.txtWeight.Text = "";
            }
            else
            {
                this.txtWeight.Text = text.ToString();
            }
        }
        private void SetSteady(object val)
        {
            if ("1".Equals(val))
            {
                this.txtWeight.Foreground = Brushes.LightGreen;
            }
            else
            {
                this.txtWeight.Foreground = Brushes.Red;
            }
        }
        private void SetPersonWeigh(object text)
        {
            this.txtPersonWeight.Text = text.ToString();
        }
        private void SetPersonSteady(object val)
        {
            if ("1".Equals(val))
            {
                this.txtPersonWeight.Foreground = Brushes.LightGreen;
            }
            else
            {
                this.txtPersonWeight.Foreground = Brushes.Red;
            }
        }
        private void AddLogTxt(object text)
        {
            this.txtLog.Text += text.ToString() + "\n";
        }
        private void SetBillInfo(Object obj)
        {
            Bill billObj = (Bill)obj;
            this.txtBillCode.Text = billObj.orderCode;
            this.txtProductName.Text = billObj.materialName;
            this.txtPiZhong.Text = billObj.pizhong;
            this.txtMaoZhong.Text = billObj.maozhong;
            this.txtJingZhong.Text = billObj.jingzhong;

            this.txtCorpName.Text = billObj.corpName;
            this.txtPosition.Text = billObj.loadPlace;

            this.txtPlanNum.Text = billObj.planNum;
            this.txtCarNo.Text = billObj.carNo;
            this.txtVideoCarNo.Text = billObj.videoCarNo;

            JObject jsonObj = JObject.FromObject(billObj);
        }
        private void AddLogMessage(Object obj)
        {
            LogInfo log = (LogInfo)obj;
            this.txtLog.Text = log.message;
            if (log.waring)
            {
                this.txtLog.Foreground = Brushes.Red;
                //LogObject.WriteLog(log.message, "Error");
            }
            else
            {
                this.txtLog.Foreground = Brushes.LightGreen;
                //LogObject.WriteLog(log.message);
            }
            if (this.txtLog.Text.StartsWith("车号"))
            {
                var msg = this.txtLog.Text;
                var carNo = msg.Substring(2);
                var newCarNo = "";
                for(int i=0; i<carNo.Length; i++)
                {
                    newCarNo += carNo.Substring(i, 1) + " ";
                }
                //speak("车号: " + newCarNo);
                speak(msg);
            }
            else
            {
               String errorMsg= this.txtLog.Text;
              MatchCollection mc = Regex.Matches(errorMsg, @"([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1})");
                foreach (Match m in mc)
                {
                    string s = m.Groups[0].Value;
                    string x = "";
                    for (int i = 0; i < s.Length; i++)
                    {
                        x += s.Substring(i, 1) + " ";
                    }
                    errorMsg = errorMsg.Replace(s, x);
                }
                speak(errorMsg);
            }
        }
        private void AddVoiceLogTxt(object text)
        {
            this.txtLog.Text += text.ToString() + "\n";
            speak(text.ToString());
        }
        private void speak(String message)
        {
            new Task(() =>
            {
                try
                {
                    //message = message.Replace(".", "点").Replace("重", "众");
                    speech.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
                    if (null != message)
                    {
                        speech.Speak(message, SpeechVoiceSpeakFlags.SVSFlagsAsync);
                    }
                }
                catch (Exception ex) { }
            }).Start();

        }
        private void print(object obj)
        {
            Bill bill = new Bill();
            if (App.GetConfig().IfPrint.Equals("0"))
            {
                //PrintUtils.printWeighOrder(bill);
            }
            
        }

        private void sendClientAutoWeigh()
        {
            JObject jsonObj = new JObject();
            jsonObj.Add("from", "client");
            jsonObj.Add("clientId", App.GetConfig().DeviceCode);
            jsonObj.Add("type", "UploadClientAutoWeigh");

            string _autoWeigh = "false";
            if (App.GetConfig().AutoWeigh)
            {
                _autoWeigh = "true";
            }
            else
            {
                _autoWeigh = "false";
            }
            jsonObj.Add("value", _autoWeigh);

            _syncContext.Post(SendMqMessage, jsonObj);

            JObject reqObj = new JObject();
            reqObj.Add("type", "AutoWeigh");
            reqObj.Add("value", _autoWeigh);
            SendNodeRedMessage("PostMessage/AutoWeighChange", reqObj);
            JObject reqObj1 = new JObject();
            SendNodeRedMessage("PostMessage/GetWeight", reqObj1);
            SendNodeRedMessage("PostMessage/GetPersonWeight", reqObj1);
            SendNodeRedMessage("PostMessage/GetRadar", reqObj1);
            SendNodeRedMessage("PostMessage/GetRFID", reqObj1);
        }

        private void Button_Click_XiaoPiao(object sender, RoutedEventArgs e)
        {
           // PrintUtils.printWeighOrder(new Bill());
        }

        private void Button_Click_ReadCard(object sender, RoutedEventArgs e)
        {
            JObject requestJSON = new JObject();
            requestJSON.Add("type", "ReadCard");
            String result = HttpPostHelper.doHttpPost("http://127.0.0.1:1880/PostMessage", requestJSON.ToString());
        }

        private void SendNodeRedMessage(String path,JObject reqObj)
        {
            try
            {
                String result = HttpPostHelper.doHttpPost(App.GetConfig().NodeRedUrl + path, reqObj.ToString());

            }
            catch (Exception ex) { }
        }
        private void SendMqMessage(Object obj)
        {
            try
            {
                JObject jsonObj = (JObject)obj;

                if (null != channel)
                {
                    var properties = channel.CreateBasicProperties();
                    properties.DeliveryMode = 1;

                    string message = jsonObj.ToString();

                    channel.BasicPublish("", "Queue_" + App.GetConfig().DeviceCode + "_Client", properties, Encoding.UTF8.GetBytes(message));
                }
            }catch (Exception ex) { }
        }
        private void SendMqMessagePrint(Object obj)
        {
            try
            {
                JObject jsonObj = (JObject)obj;

                if (null != channel)
                {
                    var properties = channel.CreateBasicProperties();
                    properties.DeliveryMode = 1;

                    string message = jsonObj.ToString();

                    string queueName = App.GetConfig().Remote_Print_QueueName;
                    string[] queueArray = queueName.Split(',');
                    foreach (String queue in queueArray)
                    {
                        channel.BasicPublish("", queue, properties, Encoding.UTF8.GetBytes(message));
                    }
                }
            }catch(Exception ex) { }
        }

        private void Button_Click_VoiceTest(object sender, RoutedEventArgs e)
        {
            speak("声音测试");
        }

        private void ImgLogo_Click(object sender, MouseButtonEventArgs e)
        {
            PassWindow win = new PassWindow();
            bool result = (bool)win.ShowDialog();
            if (result)
            {
                this.Close();
            }
        }

        public void timeCycle(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                Application.Current.Dispatcher.BeginInvoke(
                    new Action(() =>
                    {
                        TextProperty1 = DateTime.Now.ToString();
                        this.labelTime.Text = TextProperty1;
                    }));
            }).Start();
        }

        private void beginReadRFID(Object obj)
        {
            // 开启Timer更新接收的信息
            if (readThread_Left != null)
            {
                if (readThread_Left.IsAlive)
                {
                    readThread_Left.Abort();
                    readThread_Left.Join();
                }
            }
            if (readThread_Right != null)
            {
                if (readThread_Right.IsAlive)
                {
                    readThread_Right.Abort();
                    readThread_Right.Join();
                }
            }
            if (!CReadCode_Left()) //发送读卡命令失败
            {
                //MessageBox.Show("发送读卡命令失败!");
                //_syncContext.Post(AddLogMessage, new LogInfo("RFID发送读卡命令失败", true));
                return;
            }
            if (!CReadCode_Right()) //发送读卡命令失败
            {
                //MessageBox.Show("发送读卡命令失败!");
                //_syncContext.Post(AddLogMessage, new LogInfo("RFID发送读卡命令失败", true));
                return;
            }
            bReadCodeState = true;
            readThread_Left = new Thread(new ThreadStart(RevCodeMsgThread_Left));
            readThread_Left.Start();
            readThread_Right = new Thread(new ThreadStart(RevCodeMsgThread_Right));
            readThread_Right.Start();
        }
        private void stopReadRFID(Object obj)
        {

            if (readThread_Left != null)
            {
                if (readThread_Left.IsAlive)
                {
                    readThread_Left.Abort();
                    readThread_Left.Join();
                }
            }
            if (readThread_Right != null)
            {
                if (readThread_Right.IsAlive)
                {
                    readThread_Right.Abort();
                    readThread_Right.Join();
                }
            }

            //停止读卡
            RfidApi.SAAT_PowerOff(pHandle_Left);
            RfidApi.SAAT_PowerOff(pHandle_Right);


            JObject reqObj = new JObject();
            reqObj.Add("code", obj.ToString());
            SendNodeRedMessage("PostMessage/RFIDCode", reqObj);
        }
        private bool CloseConnLeft()
        {
            if (pHandle_Left == IntPtr.Zero)
            {
                return true;
            }

            bReadCodeState = false;
            bool bRet = RfidApi.SAAT_Close(pHandle_Left);
            if (bRet)
            {
                pHandle_Left = IntPtr.Zero;
            }
            return bRet;
        }
        private bool CloseConnRight()
        {
            if (pHandle_Right == IntPtr.Zero)
            {
                return true;
            }

            bReadCodeState = false;
            bool bRet = RfidApi.SAAT_Close(pHandle_Right);
            if (bRet)
            {
                pHandle_Right = IntPtr.Zero;
            }
            return bRet;
        }
        /// <summary>
        /// 天线号
        /// </summary>
        byte sAntenna = 1;
        /// <summary>
        /// 读卡次数，单次，循环
        /// </summary>
        int SAAT_READ_TYPE = 1;

        public bool CReadCode_Left()
        {
            byte SAAT_SCAN_TRIES = 3;

            return RfidApi.SAAT_6CReadEPCCode(pHandle_Left, sAntenna, (byte)SAAT_READ_TYPE, SAAT_SCAN_TRIES);
        }
        public bool CReadCode_Right()
        {
            byte SAAT_SCAN_TRIES = 3;

            return RfidApi.SAAT_6CReadEPCCode(pHandle_Right, sAntenna, (byte)SAAT_READ_TYPE, SAAT_SCAN_TRIES);
        }

        private void RevCodeMsgThread_Left()
        {
            int nRevMsgResult = 0;

            RevMsgStruct revMsg = new RevMsgStruct();
            int dwStart = System.Environment.TickCount;//计数时间
            while (bReadCodeState)
            {
                nRevMsgResult = CRevCodeMsg(ref revMsg, "left");
                if (nRevMsgResult == 1)//读到卡片
                {
                    String RFIDCode = revMsg.sCodeData;

                    if (rfidaCodeMap.ContainsKey(RFIDCode))
                    {
                        DateTime lastDateTime = (DateTime)rfidaCodeMap[RFIDCode];
                        TimeSpan ts1 = new TimeSpan(lastDateTime.Ticks);
                        TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                        TimeSpan ts = ts1.Subtract(ts2).Duration();
                        int dateDiff = ts.Seconds;
                        if (dateDiff < 5)
                        {
                            continue;
                        }
                        else
                        {
                            _syncContext.Post(sendRFIDMsg, RFIDCode);
                            rfidaCodeMap.Remove(RFIDCode);
                            rfidaCodeMap.Add(RFIDCode, DateTime.Now);
                        }
                    }
                    else
                    {
                        _syncContext.Post(sendRFIDMsg, RFIDCode);
                        rfidaCodeMap.Add(RFIDCode, DateTime.Now);
                    }
                    //_syncContext.Post(sendRFIDMsg, revMsg.sCodeData);
                }
            }
        }
        private void RevCodeMsgThread_Right()
        {
            int nRevMsgResult = 0;

            RevMsgStruct revMsg = new RevMsgStruct();
            int dwStart = System.Environment.TickCount;//计数时间
            while (bReadCodeState)
            {
                nRevMsgResult = CRevCodeMsg(ref revMsg, "right");
                if (nRevMsgResult == 1)//读到卡片
                {
                    String RFIDCode = revMsg.sCodeData;

                    if (rfidaCodeMap.ContainsKey(RFIDCode))
                    {
                        DateTime lastDateTime = (DateTime)rfidaCodeMap[RFIDCode];
                        TimeSpan ts1 = new TimeSpan(lastDateTime.Ticks);
                        TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                        TimeSpan ts = ts1.Subtract(ts2).Duration();
                        int dateDiff = ts.Seconds;
                        if (dateDiff < 5)
                        {
                            continue;
                        }
                        else
                        {
                            _syncContext.Post(sendRFIDMsg, RFIDCode);
                            rfidaCodeMap.Remove(RFIDCode);
                            rfidaCodeMap.Add(RFIDCode, DateTime.Now);
                        }
                    }
                    else
                    {
                        _syncContext.Post(sendRFIDMsg, RFIDCode);
                        rfidaCodeMap.Add(RFIDCode, DateTime.Now);
                    }
                    //_syncContext.Post(sendRFIDMsg, revMsg.sCodeData);
                }
            }

        }

        private void sendRFIDMsg(object obj)
        {
            JObject reqObj = new JObject();
            reqObj.Add("code", obj.ToString());
            SendNodeRedMessage("PostMessage/RFIDCode", reqObj);
        }

        public int CRevCodeMsg(ref RevMsgStruct revMsg, string direct)
        {
            byte sLength = 255;     // 
            string sData = "";
            byte[] btData = new byte[255];
            int bResult = 0;

            try
            {
                if ("left".Equals(direct))
                {
                    bResult = RfidApi.SAAT_6CRevEPCMsg(pHandle_Left, out sAntenna, btData, out sLength);
                }
                else if ("right".Equals(direct))
                {
                    bResult = RfidApi.SAAT_6CRevEPCMsg(pHandle_Right, out sAntenna, btData, out sLength);
                }
            }
            catch
            {

            }

            if (bResult == 1 && sLength != 255)
            {
                revMsg.nAntenna = (int)sAntenna;
                for (byte bytei = 0; bytei < sLength; bytei++)
                    sData += btData[bytei].ToString("X2");
                revMsg.sCodeData = sData;
                revMsg.nRepeatTime = 1;
                revMsg.tBeginTime = System.DateTime.Now;
                revMsg.tLastTime = System.DateTime.Now;
            }
            return bResult;
        }

        private void test_print(object sender, RoutedEventArgs e)
        {

            //JObject jsonParamObj = new JObject();
            //jsonParamObj = new JObject();
            //jsonParamObj.Add("from", "client");
            //jsonParamObj.Add("clientId", App.GetConfig().DeviceCode);
            //jsonParamObj.Add("type", "Complete");
            //jsonParamObj.Add("message", "3298323752222");
            //_syncContext.Post(SendMqMessage, jsonParamObj);
           // PrintUtils.printWeighOrder(new Bill());
        }

        private void sendMsg(String text, String text2, bool scroll = false,String direct="left")
        {
            String IP = "";
            if ("left".Equals(direct))
            {
                IP = App.GetConfig().Left_LED_IP;
            }else if ("right".Equals(direct))
            {
                IP = App.GetConfig().Right_LED_IP;
            }
            else
            {
                return;
            }
            bool lanNet = true;
            int nResult;
            LedDll.COMMUNICATIONINFO CommunicationInfo = new LedDll.COMMUNICATIONINFO();//定义一通讯参数结构体变量用于对设定的LED通讯，具体对此结构体元素赋值说明见COMMUNICATIONINFO结构体定义部份注示
            CommunicationInfo.LEDType = m_ledType;
            if (lanNet)
            {
                //TCP通讯********************************************************************************
                CommunicationInfo.SendType = 0;//设为固定IP通讯模式，即TCP通讯
                CommunicationInfo.IpStr = IP;//给IpStr赋值LED控制卡的IP
                CommunicationInfo.LedNumber = 1;//LED屏号为1，注意socket通讯和232通讯不识别屏号，默认赋1就行了，485必需根据屏的实际屏号进行赋值
                //广播通讯********************************************************************************
                //CommunicationInfo.SendType=1;//设为单机直连，即广播通讯无需设LED控制器的IP地址
                //串口通讯********************************************************************************
                //CommunicationInfo.SendType=2;//串口通讯
                //CommunicationInfo.Commport=1;//串口的编号，如设备管理器里显示为 COM3 则此处赋值 3
                //CommunicationInfo.Baud=9600;//波特率
                //CommunicationInfo.LedNumber=1;
            }
            //else
            //{
            //    //广义网通讯 C2M、C4M才支持
            //    CommunicationInfo.SendType = 4;//设为广义网通讯模式
            //    CommunicationInfo.networkIdStr = comboBox1.Text;//指定的网络ID
            //}

            IntPtr hProgram;//节目句柄
            hProgram = LedDll.LV_CreateProgramEx(m_ledWidth, m_ledHeight, m_ledColor, m_ledGrayLevel, 0);//注意此处屏宽高及颜色参数必需与设置屏参的屏宽高及颜色一致，否则发送时会提示错误
            //此处可自行判断有未创建成功，hProgram返回NULL失败，非NULL成功,一般不会失败

            nResult = LedDll.LV_AddProgram(hProgram, 0, 0, 1);//添加一个节目，参数说明见函数声明注示
            if (nResult != 0)
            {
                string ErrStr;
                ErrStr = LedDll.LS_GetError(nResult);
                //MessageBox.Show(ErrStr);
                return;
            }
            LedDll.AREARECT AreaRect = new LedDll.AREARECT();//区域坐标属性结构体变量
            AreaRect.left = 0;
            AreaRect.top = 0;
            AreaRect.width = m_ledWidth;
            AreaRect.height = 24;

            LedDll.LV_AddImageTextArea(hProgram, 0, 1, ref AreaRect, 1);

            LedDll.FONTPROP FontProp = new LedDll.FONTPROP();//文字属性
            FontProp.FontName = "宋体";
            FontProp.FontSize = 12;
            FontProp.FontColor = LedDll.COLOR_RED;
            FontProp.FontBold = 0;

            LedDll.PLAYPROP PlayProp = new LedDll.PLAYPROP();
            PlayProp.InStyle = 0;
            PlayProp.DelayTime = 3;
            PlayProp.Speed = 4;
            //可以添加多个子项到图文区，如下添加可以选一个或多个添加
            nResult = LedDll.LV_AddMultiLineTextToImageTextArea(hProgram, 0, 1, LedDll.ADDTYPE_STRING, text, ref FontProp, ref PlayProp, 0, 0);//通过字符串添加一个多行文本到图文区，参数说明见声明注示
            //nResult = LedDll.LV_AddMultiLineTextToImageTextArea(hProgram, 0, 2, LedDll.ADDTYPE_STRING, text2, ref FontProp, ref PlayProp, 0, 0);//通过rtf文件添加一个多行文本到图文区，参数说明见声明注示
            //nResult = LedDll.LV_AddMultiLineTextToImageTextArea(hProgram, 0, 1, LedDll.ADDTYPE_FILE, "test.txt", ref FontProp, ref PlayProp, 0, 0);//通过txt文件添加一个多行文本到图文区，参数说明见声明注示

            AreaRect.left = 0;
            AreaRect.top = m_ledHeight / 2;
            AreaRect.width = m_ledWidth;
            AreaRect.height = m_ledHeight / 2;

            //nResult = LedDll.LV_AddDigitalClockArea(hProgram, 0, 2, ref AreaRect, ref DigitalClockAreaInfo);//注意区域号不能一样，详见函数声明注示
            //nResult = LedDll.LV_QuickAddSingleLineTextArea(hProgram, 0, 2, ref AreaRect, LedDll.ADDTYPE_STRING, text2, ref FontProp, 4);

            if (!scroll)
            {
                LedDll.LV_AddImageTextArea(hProgram, 0, 2, ref AreaRect, 1);
                nResult = LedDll.LV_AddMultiLineTextToImageTextArea(hProgram, 0, 2, LedDll.ADDTYPE_STRING, text2, ref FontProp, ref PlayProp, 0, 0);//通过字符串添加一个多行文本到图文区，参数说明见声明注示
            }
            else
            {
                nResult = LedDll.LV_QuickAddSingleLineTextArea(hProgram, 0, 2, ref AreaRect, LedDll.ADDTYPE_STRING, text2, ref FontProp, 16);//快速通过字符添加一个单行文本区域，函数见函数声明注示
            }

            nResult = LedDll.LV_Send(ref CommunicationInfo, hProgram);//发送，见函数声明注示
            LedDll.LV_DeleteProgram(hProgram);//删除节目内存对象，详见函数声明注示
            if (nResult != 0)//如果失败则可以调用LV_GetError获取中文错误信息
            {
                string ErrStr;
                ErrStr = LedDll.LS_GetError(nResult);
                //MessageBox.Show(ErrStr);
            }
            else
            {
                //MessageBox.Show("发送成功");
            }
        }
    }
}
